<template>
  <div class="pageInner">
    <div class="status-bar" :style="{height:statusBarHeight + 'px'}"></div>
    <navbar class="navbar" :style="{backgroundColor: 'white' }">
      <navbar-item type="title">
        <text class="text">购物车</text>
      </navbar-item>
      <navbar-item type="right" @click="deleteHeader">
        <text class="text_right">{{ deleteTitle }}</text>
      </navbar-item>

    </navbar>
    <list v-if="haveProduct || haveInvaild">
      <cell v-if="haveProduct">
        <div class="shoppingCart-shopList mg-T20">
          <div v-for="(item,index) in shopList" :key="index">
            <div class="mg-T20 mg-B20"
                 style="display:flex;flex-direction: row;;justify-content: space-between;align-items: center;padding-left: 20px;padding-right: 20px;box-sizing: border-box">
              <div class="shoppingCart-shopList-top" @click="shopCheck(item.enterpriseId, item.checked)">
                <div :class="[item.checked?'product_chooseBox_checked':'product_chooseBox']">
                  <icon style="width: 40px;height:40px;color:#fff;font-size: 20px" content="md-checkmark"></icon>
                </div>
                <image class="shoppingCart-shopList-top-image" :src="item.enterpriseLogo"/>
                <text class="shoppingCart-shopList-top-text">{{ item.enterpriseName }}</text>
              </div>
            </div>
            <div style="padding-left: 20px;padding-right: 20px;box-sizing: border-box" class="shoppingCart-productList"
                 v-for="(productListItem,productIndex) in item.productList" :key="productIndex">
              <div class="shoppingCart-productList-top flex">
                <div class="shoppingCart-productList-top-left flex"
                     @click="productCheck(item.enterpriseId, productListItem.productId, productListItem.checked)">
                  <div :class="[productListItem.checked?'product_chooseBox_checked':'product_chooseBox']">
                    <icon style="width: 40px; height:40px; color:#ffffff;font-size: 20px" content="md-checkmark"></icon>
                  </div>
                </div>
                <div @click="toProductDetail(productListItem.productId)" class="shoppingCart-productList-top-right">
                  <image class="shoppingCart-productList-top-image" :src="productListItem.thumbnail"/>
                  <div class="shoppingCart-productList-top-title">
                    <text class="shoppingCart-productList-top-name">{{ productListItem.name }}</text>
                    <div class="shoppingCart-productList-top-tag flex"
                         @click="showBottomFrame(productListItem.productId)" v-if="productListItem.tag">
                      <div class="tagChooseBox">
                        <text class="shoppingCart-productList-top-tag-text">{{ productListItem.tag }}</text>
                        <icon style="width: 20px; height:50px;color: #a1a1a1;font-size: 20px "
                              content="ios-arrow-down"></icon>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="shoppingCart-productList-middle">
                <div class="shoppingCart-productList-middle-inner">
                  <div v-for="(msgItem,idx) in productListItem.moreMsgList" :key="idx">
                    <div class="shoppingCart-productList-middle-msg">{{ msgItem.msg }}</div>
                  </div>


                  <div class="shoppingCart-productList-account" v-if="type ==2 ">
<!--                    {{productListItem}}-->
                    <div class="shoppingCart_priceBox" v-if="productListItem.type !=2">
                      <text class="shoppingCart_priceIcon">￥</text>
                      <!--                                         -->
                      <text class="cl-r">{{ productListItem.price | price }}</text>
                    </div>
                    <div class="shoppingCart_priceBox" v-if="productListItem.type ==2">
                      <text class="shoppingCart_priceIcon">兑换券</text>
                      <!--                                         -->
                      <text class="cl-r">{{ productListItem.point | price }}</text>
                    </div>

                    <div class="shoppingCart-productList-control">
                      <div class="shoppingCart-productList-reduce"
                           @click="numReduce(item.enterpriseId, productListItem.productId, productListItem.quantity, productListItem.minLimit)">
                        <icon style="width: 50px; height:50px; " content="md-remove"></icon>
                      </div>
                      <div class="shoppingCart-productList-num"
                           @click="blur(item.enterpriseId, productListItem.productId, productListItem.quantity, productListItem.maxLimit, productListItem.minLimit, productListItem.stock,productIndex,index)">
                        <text class="shoppingCart-productList-numTitle">{{ productListItem.quantity }}</text>
                      </div>
                      <div class="shoppingCart-productList-add"
                           @click="numAdd(item.enterpriseId, productListItem.productId, productListItem.quantity, productListItem.maxLimit, productListItem.stock)">
                        <icon style="width: 50px; height:50px; " content="md-add"></icon>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </cell>

      <cell v-if="haveInvaild">
        <div class="shoppingCart-shopList mg-T20">
          <div class="mg-T20 mg-B20"
               style="display:flex;justify-content: space-between;align-items: center; flex-direction: row;">
            <div class="shoppingCart-shopList-top">
              <text class="shoppingCart-shopList-top-text">失效商品</text>
            </div>
            <div @click="clearInvaild">
              <text class="shoppingCart-shopList-top-delete">清空失效商品</text>
            </div>
          </div>
          <div v-for="(invalidProductListItem,index) in invalidProductList" :key="index"
               class="shoppingCart-productList">
            <div class="shoppingCart-productList-top flex" @click="toProductDetail(invalidProductListItem.productId)">
              <text class="shoppingCart-productList-top-invalidTag">失效</text>
              <image class="shoppingCart-productList-top-image" :src="invalidProductListItem.thumbnail"/>
              <div class="shoppingCart-productList-top-title">
                <text class="shoppingCart-productList-top-name">{{ invalidProductListItem.name }}</text>
                <div>
                  <text class="shoppingCart-productList-top-tag">{{ invalidProductListItem.msg }}</text>
                </div>
              </div>
            </div>
          </div>
        </div>
      </cell>
      <cell class="gsp"></cell>
    </list>
    <noData v-else noDataText="购物车暂无商品" haveBtn="true"></noData>
    <div class="shoppingCart-bottom">
      <div class="flex" @click="allCheck">
        <div :class="[allchecked?'product_chooseBox_checked':'product_chooseBox']">
          <icon style="width: 40px;height:40px;color:#fff;font-size: 20px" content="md-checkmark"></icon>
        </div>
        <text class="shoppingCart-bottom-allCheck">全选</text>
      </div>
      <div class="shoppingCart-bottom-summation flex" v-if="deleteTitle == '管理'">
        <div class="shoppingCart-bottom-summation-top flex">
          <text class="shoppingCart-bottom-summation-top-text">合计:</text>
          <text class="shoppingCart-bottom-summation-top-sum">￥{{ sum }}</text>
        </div>
        <div class="btn shoppingCart-button-submit" @click="productSumbit">
          <text class="shoppingCart-button-submit-text">结算</text>
        </div>
      </div>
      <div class="shoppingCard_deleteBox" v-if="deleteTitle == '完成'" @click="toDelete">
        <text class="shoppingCard_delete_title">删除</text>
      </div>
    </div>
    <bottomFrame ref="bottomFrame" class="bottomFrame" @selectedInfo="selectedInfo"></bottomFrame>
  </div>
</template>
<script>
import {getCart, del, add, update} from "@/api/cart";

import {list, find} from "@/api/product";

import noData from "@/components/noData/noData.vue";
import bottomFrame from "@/components/bottomFrame/bottomFrame.vue";
import myNavbar from "@/components/myNavbar/myNavbar.vue";
const eeui = app.requireModule("eeui");
export default {
  pageMessage: function (data) {
    // console.log('接收到的信息12：', data.message);
    if (data.message.key == 'cart' && this.getGlobalData().token != null) {
      // console.log('检查token3')
      this.cartInfoRequest()
    }
  },
  components: {
    noData,
    bottomFrame,
    myNavbar
  },
  data() {
    return {
      statusBarHeight: eeui.getStatusBarHeightPx(),
      deleteTitle: '管理',
      inputCurrentNum: "", //input的当前值
      inputfocusIndex: "", //暂存获取焦点的input的下表
      isShowSum: false, //是否显示合计
      listHeight: "",
      haveProduct: false, //判断购物车是否为空
      isShowBottomFrame: false, //底部属性弹窗的开关
      productIdToBottomFrame: "", //暂存被点击要更改属性的商品id
      deleteOff: false,
      productSelectedOff: false,
      allchecked: "", //全选的控制器
      sum: 0, //消费总额
      selectList: [],
      selectShopList: [],
      selectProductList: [],
      shopList: [], //商户列表，里面的productList是商品列表
      cartInfo: [], //购物车商品信息
      //失效的商品
      haveInvaild: false, //判断是否有失效商品
      showInvaildProduct: true,
      inputFocusOff: false, //input的获取焦点的开关
      refName: "", //获取焦点input的ref
      invalidProductList: [],
      type:2
    };
  },
  created() {
    let _this = this;
    if (this.getGlobalData().token != null) {
      _this.cartInfoRequest(data => {
        _this.formateCartData(data);
      });
    }

  },
  filters: {
    price(value) {
      var toFixedNum = Number(value).toFixed(3);
      var realVal = toFixedNum.substring(0, toFixedNum.toString().length - 1);
      return realVal;
    },
    number(value) {
      var toFixedNum = Number(value).toFixed(0);
      return toFixedNum;
    }
  },
  mounted() {
  },
  pageResume: function (data) {
    let _this = this;
    if (this.getGlobalData().token != null) {
      _this.cartInfoRequest(data => {
        _this.formateCartData(data);
      });
    }
  },
  methods: {
    //请求购物车数据的函数
    cartInfoRequest(callback) {
      let _this = this;
      let data = {uuid:this.getImei()};
      getCart(data)
          .then(res => {
            // console.info("发送购物的数据请求--");
            // console.info(res);
            //   console.info(res.type);
            if (res.type == "success") {
              this.cartInfo = res.data.cart.itemVoList;
              // console.log(this.cartInfo);
              if (callback) {
                callback(res.data.cart.itemVoList);
              }
            }
          })
          .catch(error => {
            // console.log(error);
          });
    },

    //整理购物车数据
    formateCartData(data) {
      let _this = this;
      let shopList = [];
      let invalidProductList = [];
      if (data.length > 0) {
        let shopIdList = [];
        data.map(item => {
          if (item.stock == 0 || item.isMarketable == false) {
            _this.haveInvaild = true;
            let invalidProductListItem = {};
            invalidProductListItem.id = item.id;
            invalidProductListItem.productId = item.productId;
            invalidProductListItem.goodsId = item.goodsId;
            invalidProductListItem.brandId = item.brandId;
            invalidProductListItem.productCategoryId = item.productCategoryId;
            invalidProductListItem.thumbnail = _this.thumbnail(
                item.thumbnail,
                148,
                148
            );
            invalidProductListItem.name = item.name;
            invalidProductListItem.tag = item.spec1 + " " + item.spec2;
            invalidProductListItem.price = item.price;
            invalidProductListItem.checked = item.chk;
            invalidProductListItem.quantity = item.quantity;
            invalidProductListItem.stock = item.stock;
            invalidProductListItem.isMarketable = item.isMarketable;
            if (item.stock == 0) {
              invalidProductListItem.msg = "缺货中";
            }
            if (item.isMarketable == false) {
              invalidProductListItem.msg = "商品已经下架了";
            }
            invalidProductList.push(invalidProductListItem);
          } else {
            shopIdList.push(item.enterpriseId);
          }
        });
        shopIdList = Array.from(new Set(shopIdList));
        if (shopIdList.length > 0) {
          shopIdList.map(item => {
            let shopListItem = {};
            shopListItem.enterpriseId = item;
            shopListItem.productList = [];
            shopListItem.checked = false;
            data.map(item1 => {
              if (item1.stock == 0 || item1.isMarketable == false) {
              } else {
                _this.haveProduct = true;
                shopListItem.enterpriseName = item1.enterpriseName;
                let productListItem = {};
                productListItem.id = item1.id;
                productListItem.productId = item1.productId;
                productListItem.goodsId = item1.goodsId;
                productListItem.brandId = item1.brandId;
                productListItem.productCategoryId = item1.productCategoryId;
                productListItem.thumbnail = _this.thumbnail(
                    item1.thumbnail,
                    148,
                    148
                );
                shopListItem.enterpriseLogo = _this.thumbnail(
                    item1.enterpriseLogo,
                    44,
                    44
                );
                productListItem.name = item1.name;
                productListItem.tag = item1.spec1
                    ? item1.spec1
                    : "" + " " + item1.spec2
                        ? item1.spec2
                        : "";
                productListItem.price = item1.price;
                // productListItem.checked = item1.chk;
                productListItem.checked = false;
                productListItem.minLimit = item1.minLimit;
                productListItem.maxLimit = item1.maxLimit;
                productListItem.quantity = item1.quantity;
                productListItem.stock = item1.stock;
                productListItem.type = item1.type;
                productListItem.point = item1.point;
                shopListItem.productList.push(productListItem);
              }
            });
            shopList.push(shopListItem);
          });
          // console.info("商户列表shopList");
          // console.info(shopList);
          if (shopList.length > 0) {
            shopList.map(item => {
              let shopOff = true; //用来检查该供应商下面的商品是否被全选
              if (item.productList.length > 0) {
                item.productList.map(item1 => {
                  if (item1.checked == false || item1.checked == "") {
                    shopOff = false;
                  }
                });
              }
              if (shopOff) {
                item.checked = true;
              }
            });
          }
        }
        _this.invalidProductList = invalidProductList;
        _this.shopList = shopList;
        this.calculate();
      } else {
        _this.invalidProductList = [];
        _this.shopList = [];
        _this.haveInvaild = false;
        _this.haveProduct = false;
      }
    },

    //店铺的全选按钮触发事件
    shopCheck: function (shopid, shopstate) {
      let enterpriseId = shopid;
      let shopState = shopstate;
      let cartInfo = this.shopList;
      if (shopState == "" || shopState == false) {
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
              if (item.enterpriseId == enterpriseId) {
                item.checked = true;
                  if (item.productList.length > 0) {
                    item.productList.map(item1 => {
                      console.log("111111111111111222222222223333333333333333333333")
                      console.log(item1.type)
                      if(item1.type == 0){
                        console.log("2222222222222222222222222222222222")
                        item1.checked = true;
                      }
                    });
                  }
              }
          });
        }
      } else {
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
            if (item.enterpriseId == enterpriseId) {
              item.checked = false;
              if (item.productList.length > 0) {
                item.productList.map(item1 => {
                  item1.checked = false;
                });
              }
            }
          });
        }
      }
      // console.log(cartInfo)
      this.shopList = cartInfo;
      this.calculate();
    },

    //单件商品的选中事件
    productCheck(shopid, productid, productstate) {
      let enterpriseId = shopid;
      let productId = productid;
      let productState = productstate;
      let cartInfo = this.shopList;
      if (productState == "" || productState == false) {
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
            if (item.enterpriseId == enterpriseId) {
              let shopOff = true; //用来检查该供应商下面的商品是否被全选
              // item.checked = true
              if (item.productList.length > 0) {
                item.productList.map(item1 => {
                  if (item1.productId == productId) {
                    if(item1.type == 0){
                      item1.checked = true;
                      item.productList.map(item2=>{
                        if(item2.type == 2){
                          item2.checked = false;
                        }
                      })
                    }
                    if(item1.type == 2){
                      item1.checked = true;
                      item.productList.map(item2=>{
                        if(item2.type == 0){
                          item2.checked = false;
                        }
                      })
                    }
                  }
                  if (item1.checked == false || item1.checked == "") {
                    shopOff = false;
                  }
                });
              }
              if (shopOff) {
                item.checked = true;
              }
            }
          });
        }
      } else {
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
            if (item.enterpriseId == enterpriseId) {
              item.checked = false;
              if (item.productList.length > 0) {
                item.productList.map(item1 => {
                  if (item1.productId == productId) {
                    item1.checked = false;
                  }
                });
              }
            }
          });
        }
      }
      this.shopList = cartInfo;
      this.calculate();
    },

    //全选的事件被触发
    allCheck() {
      // console.info("全选按钮被点击--");
      let state = this.allchecked;
      let cartInfo = this.shopList;
      if (state == "" || state == false) {
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
            item.checked = true;
            if (item.productList.length > 0) {
              item.productList.map(item1 => {
                if(item1.type == 0){
                  item1.checked = true;
                }
              });
            }
          });
          state = true;
        }
      } else {
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
            item.checked = false;
            if (item.productList.length > 0) {
              item.productList.map(item1 => {
                item1.checked = false;
              });
            }
          });
          state = false;
        }
      }
      this.shopList = cartInfo;
      this.allchecked = state;
      this.calculate();
    },

    //统计目前有多少个商品被选中
    calculate: function () {
      let _this = this;
      let selectShopList = [];
      let selectProductList = [];
      let sum = 0;
      let allchecked = true;
      if (this.shopList.length > 0) {
        this.shopList.map(item => {
          if (item.checked == true) {
            selectShopList.push(item.enterpriseId);
          } else {
            allchecked = false;
          }
          if (item.productList.length > 0) {
            item.productList.map(item1 => {
              let selectProductListItem = {};

              if (item1.checked == true&&item1.type == 0) {
                selectProductListItem.productId = item1.productId;
                selectProductListItem.id = item1.id;
                selectProductList.push(selectProductListItem);
                sum += item1.price * item1.quantity;
              }
              if (item1.checked == true&&item1.type == 2) {
                selectProductListItem.productId = item1.productId;
                selectProductListItem.id = item1.id;
                selectProductList.push(selectProductListItem);
                sum += item1.point * item1.quantity;
              }


            });
          }
        });
      }
      _this.sum = sum.toFixed(2);
      _this.selectShopList = selectShopList;
      _this.selectProductList = selectProductList;
      _this.allchecked = allchecked;
      eeui.postMessage({
        pageName: '',
        key: 'orderIndex',
      });
    },

    //数量减少事件
    numReduce(enterpriseid, productid, quantity, minlimit) {
      let _this = this;
      console.log("minlimit==" + JSON.stringify(minlimit));
      let enterpriseId = enterpriseid;
      let productId = productid;
      let currentNum = quantity;
      let minLimit = minlimit;
      let cartInfo = this.shopList;
      if (minLimit > 0 && currentNum <= minLimit) {
        eeui.toast({
          message: "商品数量不能再少了呦！",
          //gravity: "middle"
        });
      } else if (currentNum <= 1) {
        eeui.toast({
          message: "商品数量不能再少了呦！",
          //gravity: "middle"
        });
      } else {
        currentNum--;
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
            if (item.enterpriseId == enterpriseId) {
              if (item.productList.length > 0) {
                item.productList.map(item1 => {
                  if (item1.productId == productId) {
                    item1.quantity = currentNum;
                    item1.checked = true;
                    let data = [];
                    let dataItem = {};
                    dataItem.id = item1.id;
                    dataItem.quantity = currentNum;
                    dataItem.chk = true;
                    data.push(dataItem);
                    this.cartUpdate(data, () => {
                      _this.shopList = cartInfo;
                      _this.calculate();
                    });
                  }
                });
              }
            }
          });
        }
      }
    },

    //数量增加事件
    numAdd(enterpriseid, productid, quantity, maxLimit, stockNum) {
      let _this = this;
      let enterpriseId = enterpriseid;
      let productId = productid;
      let currentNum = quantity;
      let maxlimit = maxLimit;
      let stock = stockNum;
      let cartInfo = this.shopList;
      if (currentNum >= maxlimit && maxlimit > 0) {
        eeui.toast({
          message: "商品数量已达限购呦！",
          //gravity: "middle"
        });
      } else if (currentNum >= stock) {
        eeui.toast({
          message: "商品数量已达上限呦！",
          //gravity: "middle"
        });
      } else {
        currentNum++;
        if (cartInfo.length > 0) {
          cartInfo.map(item => {
            if (item.enterpriseId == enterpriseId) {
              if (item.productList.length > 0) {
                item.productList.map(item1 => {
                  if (item1.productId == productId) {
                    item1.quantity = currentNum;
                    item1.checked = true;
                    let data = [];
                    let dataItem = {};
                    dataItem.id = item1.id;
                    dataItem.quantity = currentNum;
                    dataItem.chk = true;
                    data.push(dataItem);
                    this.cartUpdate(data, () => {
                      _this.shopList = cartInfo;
                      this.calculate();
                    });
                  }
                });
              }
            }
          });
        }
      }
    },

    handfocus(index) {
      this.inputFocusOff = true;
      this.inputfocusIndex = index
    },

    numChange(e) {
      console.info("输入框的信息");
      console.info(e);
      this.inputCurrentNum = e.value;
    },

    //数量输入框的失焦事件
    blur(
        enterpriseid,
        productid,
        quantity,
        maxlimit,
        minlimit,
        stockNum,
        productIndex,
        index
    ) {
      let _this = this;
      eeui.input({
        title: "请输入数量",
        buttons: ["取消", "确定"],
        inputs: [{
          type: 'number',
          value: quantity,
        },]
      }, function (result) {
        console.log(result)
        if (result.status == "click" && result.title == "确定") {
          _this.inputCurrentNum = result.data[0]
          _this.inputFocusOff = false
          let cartInfo = _this.shopList;
          let enterpriseId = enterpriseid;
          let productId = productid;
          let oldNum = quantity;
          console.log(_this.inputCurrentNum)
          console.log(quantity)
          let currentNum =
              _this.inputCurrentNum == "" ? quantity : _this.inputCurrentNum;
          console.log('11111222')
          _this.shopList[index].productList[
              productIndex
              ].quantity = _this.inputCurrentNum;
          let stock = stockNum;
          let maxLimit = maxlimit;
          let minLimit = minlimit;
          if (_this.inputCurrentNum == "") {
            console.info("我进判断了==" + oldNum);
            _this.shopList[index].productList[productIndex].quantity = oldNum;
          } else if (currentNum < minLimit && minLimit != 0) {
            eeui.toast({
              message: "商品数量不能小于起售量呦！",
              //gravity: "middle"
            });
            _this.shopList[index].productList[productIndex].quantity = oldNum;
          } else if (currentNum < 1) {
            eeui.toast({
              message: "商品数量不能小于1呦！",
              //gravity: "middle"
            });
            _this.shopList[index].productList[productIndex].quantity = oldNum;
          } else if (currentNum > maxLimit && maxLimit != 0) {
            eeui.toast({
              message: "商品数量超过上限呦！",
              //gravity: "middle"
            });
            _this.shopList[index].productList[productIndex].quantity = oldNum;
          } else if (currentNum > stock) {
            eeui.toast({
              message: "商品数量超过上限呦！",
              //gravity: "middle"
            });
            _this.shopList[index].productList[productIndex].quantity = oldNum;
          } else {
            if (cartInfo.length > 0) {
              cartInfo.map(item => {
                if (item.enterpriseId == enterpriseId) {
                  if (item.productList.length > 0) {
                    item.productList.map(item1 => {
                      if (item1.productId == productId) {
                        item1.quantity = currentNum;
                        item1.checked = true;
                        let data = [];
                        let dataItem = {};
                        dataItem.id = item1.id;
                        dataItem.quantity = currentNum;
                        dataItem.chk = true;
                        data.push(dataItem);
                        _this.cartUpdate(data, () => {
                          _this.shopList = cartInfo;
                          _this.calculate();
                        });
                      }
                    });
                  }
                }
              });
            }

            let selectList = [];
            _this.cartInfoRequest(data => {
              let sendData = [];
              let shopList = _this.shopList;
              if (data.length > 0) {
                data.map(item => {
                  let sendDataItem = {};
                  if (shopList.length > 0) {
                    shopList.map(item1 => {
                      if (item1.productList.length > 0) {
                        item1.productList.map(item2 => {
                          if (item.id == item2.id) {
                            sendDataItem.id = item.id;
                            sendDataItem.chk = item2.checked;
                            sendDataItem.quantity = item2.quantity;
                            sendData.push(sendDataItem);
                            if (item2.checked) {
                              selectList.push(item.id);
                            }
                          }
                        });
                      }
                    });
                  }
                });
              }
              if (selectList.length > 0) {
                _this.cartUpdate(sendData, () => {
                  eeui.openPage({
                        url: 'root://pages/goods/submit/index.js',
                        statusBarType: 'immersion',
                        statusBarStyle: false,
                      },
                      (res) => {
                        if (res.status == "stop") {
                          _this.cartInfoRequest(data => {
                            _this.formateCartData(data);
                          });
                        }
                      }
                  );
                });
              } else {
                eeui.toast({
                  message: "请选择需要的商品",
                  //gravity: "middle"
                });
              }
            });
          }
        }
      });
    },

    //跳转商品详情页
    toProductDetail(productid) {
      var _this = this
      eeui.openPage({
            params: productid,
            pageName: 'details',
            url: 'root://pages/goods/product/details.js',
            statusBarType: 'immersion',
            statusBarStyle: false,
          },
          (res) => {
            if (res.status == "stop") {
              _this.cartInfoRequest(data => {
                _this.formateCartData(data);
              });
            }
          }
      )
    },


    //调用购物车的数量更新接口
    cartUpdate(data, callback) {
      console.info("购物车数量变更请求的请求--");
      data = JSON.stringify(data)
      update(data).then(res => {
        console.log(res.type)
        //
        if (res.type == "success") {
          if (callback) {
            callback();
          }
        }
      })
          .catch(error => {
            console.info(error);
            eeui.toast({
              message: "服务器开小差了，请重试",
              //gravity: "middle"
            });
          });
    },

    //调用购物车的添加接口
    cartAdd(productId, num, callback) {
      //发送添加购物车的请求
      let data = {};
      data.productId = productId;
      data.promotionId = "";
      data.quantity = num;
      data.uuid = this.getGlobalData().uuid;
      console.log("商品加入购物车的请求data--" + JSON.stringify(data));
      add(data)
          .then(res => {
            console.log("商品加入购物车的请求--" + JSON.stringify(res));
            if (res.type == "success") {
              if (callback) {
                callback(res.data);
              }
            }
          })
          .catch(error => {
            console.log(error);
            eeui.toast({
              message: "服务器开小差了，请重试",
              //gravity: "middle"
            });
          });
    },

    //调用购物车的删除的接口
    cartDelete(data, type, callback) {
      var _this = this
      console.info("选中的商品列表selectShopList--" + JSON.stringify(data));
      let ids = "";
      let idList = [];
      if (data.length > 0) {
        idList = [];
        data.map(item => {
          idList.push(item.id);
        });
        ids = idList.join(",");
        del(ids)
            .then(res => {
              console.log("购物车删除商品的请求--" + JSON.stringify(res));
              eeui.postMessage({
                pageName: '',
                key: 'orderIndex',
              });
              if (res.type == "success") {
                let shopList = this.shopList;
                let shopIndex = "";
                let productIndex = "";
                if (type == "del") {
                  data.map((item, index) => {
                    if (shopList.length > 0) {
                      shopList.map((item1, index1) => {
                        if (item1.productList.length > 0) {
                          item1.productList.map((item2, index2) => {
                            if (item.id == item2.id) {
                              shopIndex = index1;
                              productIndex = index2;
                            }
                          });
                        }
                      });
                      shopList[shopIndex].productList.splice(productIndex, 1);
                    }
                    if (shopList[shopIndex].productList.length == 0) {
                      shopList.splice(shopIndex, 1);
                    }
                  });
                  let haveProduct = this.haveProduct;
                  if (shopList.length == 0) {
                    this.haveProduct = false;
                  }
                  this.shopList = shopList;
                } else if (type == "change") {
                  if (callback) {
                    callback();
                  }
                }
              }
            })
            .catch(error => {
              console.log(error);
              eeui.toast({
                message: "服务器开小差了，请重试",
                //gravity: "middle"
              });
            });
      }
    },
    deleteHeader() {
      var _this = this
      if (_this.deleteTitle == '管理') {
        _this.deleteTitle = '完成'
      } else {
        _this.deleteTitle = '管理'
      }
    },
    //删除选中商品
    toDelete(e) {
      let _this = this;
      let num = this.selectProductList.length;
      if (num > 0) {
        eeui.confirm(
            {
              title: "",
              message: "确定将这" + num + "个宝贝删除？",
              buttons: ["我在想想", "删除"],
            },
            function (result) {
              if (result.status == "click" && result.title == "删除") {
                if (num > 0) {
                  _this.cartDelete(_this.selectProductList, "del");
                }
              }
            }
        );
      } else {
        eeui.toast({
          message: "未选择要删除的商品",
          //gravity: "middle"
        });
      }
    },

    //清空失效商品
    clearInvaild() {
      let _this = this;
      eeui.confirm(
          {
            title: "温馨提示",
            message: "您确定清空失效商品吗？",
            buttons: ["取消", "确定"]
          },
          function (result) {
            if (result.status == "click" && result.title == "确定") {
              let idsList = [];
              let ids = "";
              if (_this.invalidProductList.length > 0) {
                _this.invalidProductList.map(item => {
                  idsList.push(item.id);
                });
                ids = idsList.join(",");
              }
              del(ids)
                  .then(res => {
                    if (res.type == "success") {
                      (_this.invalidProductList = []), (_this.haveInvaild = false);
                    }
                  })
                  .catch(error => {
                    eeui.toast({
                      message: "服务器开小差了，请重新添加商品",
                      //gravity: "middle"
                    });
                  });
            }
          }
      );
    },

    //跳转订单确认页面
    productSumbit() {
      let _this = this;
      if (this.inputFocusOff) {
        _this.$refs.input[this.inputfocusIndex].blur()
      } else {
        let selectList = [];
        this.cartInfoRequest(data => {
          let sendData = [];
          let shopList = _this.shopList;
          if (data.length > 0) {
            data.map(item => {
              let sendDataItem = {};
              if (shopList.length > 0) {
                shopList.map(item1 => {
                  if (item1.productList.length > 0) {
                    item1.productList.map(item2 => {
                      if (item.id == item2.id) {
                        sendDataItem.id = item.id;
                        sendDataItem.chk = item2.checked;
                        sendDataItem.quantity = item2.quantity;
                        sendData.push(sendDataItem);
                        if (item2.checked) {
                          selectList.push(item.id);
                        }
                      }
                    });
                  }
                });
              }
            });
          }
          if (selectList.length > 0) {

            _this.cartUpdate(sendData, () => {
              eeui.openPage({
                    url: 'root://pages/goods/submit/index.js',
                    statusBarType: 'immersion',
                    statusBarStyle: false,
                  },
                  (res) => {
                    if (res.status == "stop") {
                      _this.cartInfoRequest(data => {
                        _this.formateCartData(data);
                      });
                    }
                  }
              );
            });
          } else {
            eeui.toast({
              message: "请选择需要的商品",
              //gravity: "middle"
            });
          }
        });
      }

    },

    //打开属性选择弹窗
    showBottomFrame(productid) {
      let _this = this;
      console.info("我被点击了productid" + productid);
      this.productIdToBottomFrame = productid;
      console.info(
          "被选中的productIdToBottomFrame" + this.productIdToBottomFrame
      );
      this.$refs.bottomFrame.showFrame(
          _this.productIdToBottomFrame,
          "changeAttr"
      );
    },

    //属性选择弹窗完成后数据回传函数
    selectedInfo(data) {
      let _this = this;
      console.info("被选中要替换的商品--" + this.productIdToBottomFrame);
      console.info("被选中要替换的商品--" + JSON.stringify(this.shopList));
      console.info("属性数据回收了--" + JSON.stringify(data));
      let shopList = this.shopList;
      let productoff = true; //查询所选的商品是不是已经存在
      let shopIndex = "";
      let productIndex = "";
      let selectProductList = [];
      if (shopList.length > 0) {
        shopList.map((item, index) => {
          if (item.productList.length > 0) {
            item.productList.map((item1, index1) => {
              if (item1.productId == data.productId) {
                productoff = false;
                if (item1.maxLimit == 0) {
                  item1.quantity += data.currentNum;
                } else if (
                    item1.quantity + data.currentNum <
                    item1.maxLimit
                ) {
                  item1.quantity += data.currentNum;
                } else {
                  eeui.toast({
                    message: "商品数量超过上限",
                    //gravity: "middle"
                  });
                }
              }
              if (this.productIdToBottomFrame == item1.productId) {
                console.info(
                    "被选中要替换的商品item1--" + JSON.stringify(item1)
                );
                shopIndex = index;
                productIndex = index1;
                let selectProductListItem = {};
                selectProductListItem.id = item1.id;
                selectProductListItem.productId = item1.productId;
                selectProductList.push(selectProductListItem);
              }
            });
          }
        });
        _this.shopList = shopList;
        console.info(
            "被选中的selectProductList--" + JSON.stringify(selectProductList)
        );
        if (this.productIdToBottomFrame != data.productId) {
          _this.cartDelete(selectProductList, "change", () => {
            if (productoff) {
              this.cartAdd(data.productId, data.currentNum, res => {
                console.log("属性替换成功后的返回res--" + JSON.stringify(res));
                let productListItem = {};
                productListItem.id = res.id;
                productListItem.productId = res.productId;
                productListItem.goodsId = res.goodsId;
                productListItem.brandId = res.brandId;
                productListItem.productCategoryId = res.productCategoryId;
                productListItem.thumbnail = _this.thumbnail(res.thumbnail, 148, 148);
                productListItem.name = res.name;
                productListItem.tag = res.spec1 ? res.spec1 : '' + " " + res.spec2 ? res.spec2 : ''
                productListItem.price = res.price;
                productListItem.checked = res.chk;
                productListItem.quantity = res.quantity;
                console.log(
                    "单件商品数据productListItem--" +
                    JSON.stringify(productListItem)
                );
                shopList[shopIndex].productList.splice(
                    productIndex,
                    1,
                    productListItem
                );
                console.log("shopList--" + JSON.stringify(shopList));
                _this.shopList = shopList;
                _this.calculate()
              });
            }
          });
        }
      }
    },

    //空购物车时，跳转到首页
    toIndex() {
      eeui.openPage({
            url: 'root://pages/index/index',
            statusBarType: 'immersion',
            statusBarStyle: false,
          },
      );
    }
  }
};
</script>
<style lang="css" src="../../style/wx.css"/>
<style scoped>
page {
  background-color: rgba(241, 242, 243, 1);
}

.pageInner {
  flex: 1;
  background-color: rgba(248, 248, 248, 1);
}

.shoppingCart-shop {
  border: solid 1px black;
}

.shoppingCart-shopList {
  margin: 0 20px 20px;
  background-color: rgba(255, 255, 255, 1);
  border-radius: 16px;
  /*padding: 10px 20px;*/
}

.shoppingCart-shopList-top {
  height: 50px;
  display: flex;
  flex-direction: row;
  align-items: center;
}

.shoppingCart-shopList-top-image {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-left: 20px;
  margin-right: 10px;

}

.shoppingCart-shopList-top-text {
  line-height: 50px;
  font-size: 28px;
  font-weight: 700;
}

.shoppingCart-shopList-top-delete {
  line-height: 50px;
  font-size: 24px;
  color: #a1a1a1;
}

.shoppingCart-productList {
  padding-top: 20px;
  padding-bottom: 10px;
}

.shoppingCart-productListTwo {
  padding-top: 20px;
  padding-bottom: 10px;
  background-color: rgba(248, 249, 250, 1);
}

.shoppingCart-productList-top-left {
  height: 150px;
}

.shoppingCart-productList-top-right {
  display: flex;
  flex-direction: row;
}

.shoppingCart-productList-top-image {
  width: 148px;
  height: 148px;
  margin: 0 20px;
  border-radius: 6px;
  background-color: #a1a1a1;
}

.shoppingCart-productList-top-title {
  width: 405px;
  /* height: 148px; */
  margin-left: 15px;
}

.shoppingCart-productList-top-name {
  font-size: 30px;
  font-family: PingFang SC;
  font-weight: 700;
  max-height: 80px;
  color: rgba(26, 26, 30, 1);
  line-height: 40px;
  lines: 2;
  text-overflow: ellipsis;
}

.shoppingCart-productList-top-invalidTag {
  font-size: 20px;
  border-radius: 6px;
  padding: 2px 4px;
  color: #fff;
  letter-spacing: 2px;
  background-color: #a1a1a1;
  /* width: 40px; */
}

.shoppingCart-productList-top-tag {
  padding-top: 10px;
}

.shoppingCart-productList-top-tag-text {
  border-radius: 10px;
  font-size: 26px;
  font-family: PingFang SC;
  font-weight: 400;
  max-width: 350px;
  color: #a1a1a1;
  text-overflow: ellipsis;
  lines: 2;
  margin-right: 10px;
}

.shoppingCart-productList-arrow {
  color: #a1a1a1;
  margin-right: 15px;
}

.shoppingCart-productList-middle {
  display: flex;
  margin-top: 20px;
  flex-direction: row-reverse;
  /*
        justify-content: space-between;
        align-items: center; */
}

.shoppingCart-productList-middle-inner {
  width: 420px;
}

.shoppingCart-productList-middle-msg {
  font-size: 24px;
  color: #ff4000;
  margin-bottom: 16px;
}

.shoppingCart-productList-account {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

/* .shoppingCart-productList-price {

} */

.cl-r {
  color: #ff4000;
  font-weight: bold;
  font-size: 36px;
  display: block;
  line-height: 32px;
}

.cl-b {
  font-size: 30px;
  color: #000;
  font-weight: bold;
}

.shoppingCart-productList-control {
  display: flex;
  flex-direction: row;
  border-radius: 12px;
  /* align-items: center; */
  border-style: solid;
  border-color: rgba(151, 150, 150, 0.493);
  border-width: 1px;
  margin-bottom: 6px;
}

.shoppingCart-productList-reduce {
  text-align: center;
  width: 50px;
  height: 50px;
  /*line-height: 50px;*/
  font-size: 24px;
  border-right-style: solid;
  border-right-width: 1px;
  border-right-color: rgba(151, 150, 150, 0.493);
}

.shoppingCart-productList-num {
  text-align: center;
  height: 50px;
  width: 80px;
  /* min-width: 100px; */
  /* max-width: 130px; */
  /* line-height: 60px; */
  font-size: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.shoppingCart-productList-numTitle {
  font-size: 30px;
}

.shoppingCart-productList-add {
  text-align: center;
  height: 50px;
  width: 50px;
  /*line-height: 50px;*/
  font-size: 24px;
  border-left-style: solid;
  border-left-width: 1px;
  border-left-color: rgba(151, 150, 150, 0.493);
}

/* 底部的固定框 */
.shoppingCart-bottom {
  width: 750px;
  height: 100px;
  display: flex;
  flex-direction: row;
  padding: 10px 20px;
  position: fixed;
  bottom: 0px;
  background-color: #fff;
  justify-content: space-between;
  align-items: center;
  z-index: 100;
  border-bottom-color: rgba(241, 242, 243, 1);
  border-bottom-style: solid;
  border-bottom-width: 1px;
}

.shoppingCart-bottom-allCheck {
  /* border: solid 1px #000; */
  margin-left: 10px;
  color: #a1a1a1;
  font-size: 26px;
  line-height: 50px;
}

.shoppingCart-bottom-summation-top {
  margin-right: 20px;
  max-width: 350px;
  margin-top: 4px;
}

.shoppingCart-bottom-summation-top-text {
  font-size: 30px;
}

.shoppingCart-bottom-summation-top-sum {
  font-size: 24px;
  color: #ff4000;
}

.shoppingCart-button-submit {
  background-image: linear-gradient(to top, #ff560c, #fe8003);
  height: 70px;
  width: 150px;
  border-radius: 100px;
}

.shoppingCart-button-submit-text {
  line-height: 70px;
  text-align: center;
  font-size: 30px;
  font-weight: normal;
  color: #fff;
}

.shoppingCart-button-delete {
  border: solid 1px #ff4000;
  color: #ff4000;
  font-weight: normal;
  font-size: 32px;
  line-height: 60px;
  height: 80px;
  padding: 10px 36px;
  border-radius: 16px;
}

.shoppingCart-none {
  border-style: solid;
          border-width: 1px;
          border-color: #000;
  /* position: absolute; */
  /* margin: 0 auto;
  top: 35px; */
  width: 750px;
  /* height: 350px; */
  /* width: 100%; */
  text-align: center;
}

.shoppingCart-none-inner {
  border-style: solid;
          border-width: 1px;
          border-color: #000;
  margin: 0 auto;
  width: 300px;
  text-align: center;
}

.shoppingCart-none-image {
  text-align: center;
  display: block;
  margin: 0 auto;
  width: 196px;
  /* height:131px; */
  height: 200px;
}

.shoppingCart-none-text {
  text-align: center;
  letter-spacing: 5px;
  font-size: 28px;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(26, 26, 30, 1);
  line-height: 42px;
  opacity: 0.3;
  margin-top: 30px;
}

.shoppingCart-none-button {
  font-size: 32px;
  /* display: block; */
  height: 82px;
  margin-top: 30px;
  /* color: #fff; */
  /* background-image: linear-gradient(to top, #FF560C , #FE8003); */
  border-radius: 16px;
  letter-spacing: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 底部填充的空白块 */
.gsp {
  height: 80px;
}

.product_chooseBox {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-color: #a1a1a1;
  border-style: solid;
  border-width: 1px;
}

.product_chooseBox_checked {
  background-color: #ff8000;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-color: white;
  border-style: solid;
  border-width: 1px;
}

.page {
  position: relative;
}

.tagChooseBox {
  display: flex;
  flex-direction: row;
  align-items: center;
  background-color: #f8f8f8;
  border-radius: 8px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 20px;
  padding-right: 20px;
}

.shoppingCart_priceBox {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
}

.shoppingCart_priceIcon {
  font-size: 24px;
  color: #ff4000;
  font-weight: bold;
  line-height: 25px;
}

.navbar {
  width: 750px;
  height: 100px;
}

.text {
  font-size: 36px;
}

.status-bar {
  background-color: white;
}

.text_right {
  font-size: 26px;
  padding-right: 20px;
  padding-left: 20px;
}

.shoppingCard_deleteBox {
  width: 100px;
  height: 50px;
  /*margin-right: 20px;*/
  border-width: 2px;
  border-style: solid;
  border-color: red;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 100px;
}

.shoppingCard_delete_title {
  font-size: 24px;
  line-height: 50px;
  color: red;
}
</style>
